package com.example.designpatternproject.wrapper;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.base.BaseActivity;
import com.example.designpatternproject.R;
import com.example.designpatternproject.databinding.ActivityWrapperBinding;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @ClassName: WrapperActivity
 * @Author: 史大拿
 * @CreateDate: 10/19/22$ 3:29 PM$
 * TODO 装饰器设计模式 (给recyclerView添加头尾布局)
 */
@SuppressLint({"DefaultLocale", "InflateParams", "SetTextI18n"})
public class WrapperActivity extends BaseActivity<ActivityWrapperBinding> {

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void initView(Bundle savedInstanceState) {

        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));

        RecyclerAdapter originAdapter = new RecyclerAdapter(getData());

        WrapperAdapter wrapperAdapter = new WrapperAdapter(this, originAdapter);
        binding.recyclerView.setAdapter(wrapperAdapter);

        addHeadView(wrapperAdapter);

        addFootView(wrapperAdapter);

        // 删除head布局0
//        wrapperAdapter.removeHeadView(0);


        wrapperAdapter.seOnClickListener(new WrapperAdapter.OnAdapterListener() {
            @Override
            public void headViewClick(View view, int position) {
                showToast("head:" + position);
//                wrapperAdapter.removeHeadView(view);
                wrapperAdapter.removeHeadView(position);
            }

            @Override
            public void centerViewClick(View view, int position, int adapterPosition) {
                showToast("center:" + position + "\tadapterPosition:" + adapterPosition);
                /*
                 * 作者:史大拿
                 * 创建时间: 10/20/22 3:53 PM
                 * TODO 如果存在footView，那么删除数据时候会有问题
                 * 导致的原因是: RecyclerView#onCreateViewHolder() 每一个ViewHolder只会走一次，一旦删除，
                 *             和RecyclerView#onBindViewHolder() 就对不上了! onBindVieHolder() 会执行多次!
                 *
                 * 这里只是为了使用装饰者模式，给rv添加headView / footView，就不纠结这个bug了! (其实是找不到更好的办法，但是可以参考BaseQuickAdapter)
                 *
                 */
//                originAdapter.removeData(position);

            }

            @Override
            public void footViewClick(View view, int position, int adapterPosition) {
//                wrapperAdapter.removeFootView(view);
                wrapperAdapter.removeFootView(position);

                showToast("foot:" + position + "\tadapterPosition:" + adapterPosition);
            }
        });
    }

    private void showToast(Object msg) {
        Toast.makeText(getApplicationContext(), msg.toString(), Toast.LENGTH_SHORT).show();
    }

    //region 添加foot布局
    /*
     * 作者:史大拿
     * 创建时间: 10/19/22 4:32 PM
     */
    private void addFootView(WrapperAdapter wrapperAdapter) {
        View view4 = LayoutInflater.from(this).inflate(R.layout.item_wrapper_item, null);
        TextView textView4 = view4.findViewById(R.id.tv_item);
        textView4.setText("footView444");
        wrapperAdapter.addFootView(view4);

        View view5 = LayoutInflater.from(this).inflate(R.layout.item_wrapper_item, null);
        TextView textView5 = view5.findViewById(R.id.tv_item);
        textView5.setText("footView555");
        wrapperAdapter.addFootView(view5);
    }
    //endregion

    //region 添加head布局
    /*
     * 作者:史大拿
     * 创建时间: 10/19/22 4:32 PM
     */
    private void addHeadView(WrapperAdapter wrapperAdapter) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_wrapper_item, null, false);
        TextView textView = view.findViewById(R.id.tv_item);
        textView.setText("headView1111");
        wrapperAdapter.addHeadView(view);

        View view2 = LayoutInflater.from(this).inflate(R.layout.item_wrapper_item, null, false);
        TextView textView2 = view2.findViewById(R.id.tv_item);
        textView2.setText("headView222");
        wrapperAdapter.addHeadView(view2);
//
//
        View view3 = LayoutInflater.from(this).inflate(R.layout.item_wrapper_item, null);
        TextView textView3 = view3.findViewById(R.id.tv_item);
        textView3.setText("headView333");
        wrapperAdapter.addHeadView(view3);
    }
    //endregion

    @RequiresApi(api = Build.VERSION_CODES.N)
    private List<String> getData() {
        return IntStream.range(0, 20).mapToObj(it -> "position" + it).collect(Collectors.toList());
    }
}
